/**
 * 主功能页面，用于设置行业
 * author:zhushy
 * date:2016年8月22日22:30:42
 */
(function(){
	var kmMain = {
		/**
		 * bindEvent 页面事件
		 */
		bindEvent:function(){
			//跳转搜索
			$("#search_on_input").unbind().on("click", function(){
				window.sessionStorage.setItem("clear_input",true);
				window.sessionStorage.removeItem("searchKey");
				window.location.href = "#product-list";
			});
			//跳到收藏
			$("#collection").unbind().on("click",function(){
				//跳转
				window.location.href = "#collect-list";
			});
			
			//跳转设置
			$(".set-up").unbind().on("click",function(){
				window.location.href = "#selectIndustry";
				log("测试");
			});
		},
		/**
		 * 
		 */
		loadData:function(){
			//从首页内存中取得设置的列表
			var userSettingJson = window.sessionStorage.userSettingJson;
			//转换为JSON
			var res = common.strToJson(userSettingJson);
			//显示数据
			if (res.data && res.data.length>0){
				 kmMain.showUserSetting(res.data);
			}else{
				$("#knowledge-base-list").html("");
				common.show_nodata("当前无设置信息","knowledge-base-list");
			}
			// 从内存去最近搜索
			var localStorageGetKeywords = window.localStorage.getItem("keywords");
			var keywordArray = JSON.parse(localStorageGetKeywords);
			//没有搜索 隐藏
			if(!keywordArray){
				$("#search-resent").hide();
			}else{
				$("#search-resent").show();
				//倒序
				keywordArray.reverse();
				var html= '<div class="set-list-h1">最近搜索</div><div class="knowledge-second">';
				for (var i=0; i < keywordArray.length; i++) {
					//空字符串跳过
					if (keywordArray[i].length == 0) {
						continue;
					}
			        html +='<div class="knowledge-search-item">'+keywordArray[i]+'</div>';  
			    }
			    html += '</div>';
			    $("#search-resent").html(html);
                //跳转搜索结果
			    $(".knowledge-search-item").unbind().on("click",function(e){
			    	var key =e.currentTarget.innerText;
			    	window.sessionStorage.searchKey = key;
					window.sessionStorage.setItem("clear_input",false);
				    window.location.href = "#product-list/type1";
			    });
			}
		},
		/**
		 * 用于显示用户设置
		 */
		showUserSetting:function(data){
			//拼接显示内容
		    var html = '<div class="knowledge-base-count">';
		    for(var i=0;i<data.length;i++){
		    	var industry = data[i];
		    	if (industry.list.length>0) {
		    		html +='<div class="set-list-h1"><span class="set-list-span"></span>'+industry.cate_name+'</div>';
				    for(var j=0;j<industry.list.length;j++){
				    	var step= industry.list[j];
				    	if (step.list.length>0) {
				    		html +='<div class="set-list-h2"><div class="set-list-h3">'+step.cate_name+'</div><div class="knowledge-second">'
				    		for (var k=0; k < step.list.length; k++) {
				    		  var equip = step.list[k];
				    		  //console.log(equip);
				    		  html +='<div class="knowledge-item" id="'+equip.pk_industry_chart+'" industry_name= "'+equip.cate_name+'">'+equip.cate_name+'</div>';
				    		};
				    		html +='</div>';
				    	};
				    	// html +='<div class="knowledge-item" id="'+data[i].list[j].pk_industry_chart+'" industry_name= "'+data[i].cate_name+'">'+data[i].cateList[j].cate_name+'</div>';
				    }
				    html+='</div></div><div class="margin-gray"></div>';
		    	};
		    }
		    //显示
		    $("#knowledge-base-list").html(html);
			//绑定事件
			//跳转知识导航
			$(".knowledge-item").unbind().on("click",function(e){
				//跳转
				var pk_industry_chart = e.currentTarget.id;
				var industry_name = e.currentTarget.attributes.industry_name.nodeValue;
				var innerText =e.currentTarget.innerText;
				pk_industry_chart = pk_industry_chart +","+innerText;
				window.sessionStorage.searchKey = industry_name;
				window.sessionStorage.pk_industry_chart = pk_industry_chart;
				window.location.href = "#compet-list";
			});

		}
	};
	$(document).ready(function(){
		//common.showLoading();
		//common.showTip("设置行业加载成功");
		kmMain.loadData();
		kmMain.bindEvent();
	});
})();
